<?php
/***
 * 描述：商品价格视图
 * @category		Lib
 * @package			Model			
 * @subpackage	
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-7-30
 * @version			$Id: ProductPriceViewModel.class.php 76 2013-12-01 14:53:31Z whtaok@gmail.com $
 */
class ProductPriceViewModel extends CommonViewModel{
	//商品价格模型字段
	public $productPriceViewFields = array(
			'id'			=>'id',
			'shop_id'		=>'shop_id',
			'price_real'	=>'price_real',
			'price_sale'	=>'price_sale',
			'stock_qty'		=>'stock_qty',
			'is_hot'		=>'is_hot',
			'is_recommend'	=>'is_recommend',
			'is_show'		=>'is_show',
			'is_new'		=>'is_new',
			'is_foreign'	=>'is_foreign',
			'ordlist'		=>'ordlist',
		);
	//商品基本表模型字段
	public $productViewFields = array(
			'id'			=>'prod_id',
			'name'			=>'name',
			'title'			=>'title',
			'brand'			=>'brand',
			'cate_code1'	=>'cate_code1',
			'cate_code2'	=>'cate_code2',
			'spec'			=>'spec',
			'spec_unit'		=>'spec_unit',
			'spec_qty'		=>'spec_qty',
			'stock_prod_id'	=>'stock_prod_id',
			'is_show'		=>'base_is_show',
	);
	
	/**
	 * 描述：设置查询已定价商品的SQL
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $shop_id		商铺ID
	 * @since		2013-4-21
	 */
	public function buildPricedFields($shop_id){
		$productPriceViewFields = $this->productPriceViewFields;
		$productViewFields = $this->productViewFields;
		$productViewFields['_on'] = 'ProductPrice.prod_id = ProductLib.id and ProductPrice.shop_id = '.$shop_id;
		$this->viewFields=array(
			'ProductPrice' => $productPriceViewFields,
			'ProductLib' 	=>$productViewFields
		);
		return $this;
	}
	
	/**
	 * 描述：设置查询未定价商品的SQL
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $shop_id		商铺ID
	 * @since		2013-4-21
	 */
	public function buildUnpricedFields($shop_id){
		$productViewFields = $this->productViewFields;
		$productViewFields['_type'] = 'LEFT';
		$productPriceViewFields = $this->productPriceViewFields;
		$productPriceViewFields['_on'] = 'ProductPrice.prod_id = ProductLib.id and ProductPrice.shop_id = '.$shop_id;
		$this->viewFields=array(
				'ProductLib' 	=>$productViewFields,
				'ProductPrice' => $productPriceViewFields
		);
		return $this;
	}
	
	/**
	 * 描述：根据查询条件，查询指定商铺的商品详细信息列表，带分页
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $shop_id		商铺ID
	 * @param 		array  $search		商品ID数组
	 * @param 		string $order		排序
	 * @since		2013-4-21
	 */
	public function findListPage($shop_id,$search,$order='is_hot desc,is_recommend desc,ordlist'){
		$modWhere = array(
			'is_show'=>1, //上架店铺开关
			'base_is_show'=>1 //上架总开关
		);
		if($search['name']){
			$names = explode(' ',$search['name']);
			foreach ($names as $key=>$value){
				if($names[$key] == ''){
					unset($names[$key]);
				}else{
					$names[$key] = '%'.$value.'%';
				}
			}
			$modWhere['name']=array('like',$names,'OR');
			
		}else if($search['cate_code']){
			$cate_codes = split("-", $search['cate_code']);
			if(count($cate_codes)>1){
				$modWhere['cate_code2']=array('in',$cate_codes);
			}else{
				$modWhere['cate_code2']=array('like',$search['cate_code'].'%');
			}
		}
		if($search['brand']){
			$modWhere['brand']=array('like',$search['brand'].'%');
		}
		
		if($search['stock_prod_id']===0){
			$modWhere['stock_prod_id']=0;
		}
		
		if($search['is_hot']){
			$modWhere['is_hot'] =1;
		}
		if($search['is_rmd']){
			$modWhere['is_recommend'] =1;
		}
		if($search['is_new']){
			$modWhere['is_new'] =1;
		}
		if($search['is_frn']){
			$modWhere['is_foreign'] =1;
		}
		$this->buildPricedFields($shop_id);
		$totalRows = $this->where($modWhere)->count();
		$rtnData = $this->where($modWhere)->order($order)->listPage($totalRows);
		return $rtnData;
	}
	
	/**
	 * 描述：根据商品ID列表，查询指定商铺的商品详细信息列表
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $shop_id		商铺ID
	 * @param 		array  $prod_ids	商品ID数组
	 * @since		2013-4-21
	 */
	public function findListByProdIds($shop_id,$prod_ids=array()){
		$search['is_show'] = 1;			//上架店铺开关
		$search['base_is_show'] = 1; 	//上架总开关
		$search['prod_id'] = array('in',$prod_ids);
		$this->buildPricedFields($shop_id);
		$list = $this->setModWhere($search)->select();
		return $list;
	}
	
	public function findCateBrandList($shop_id,$cate_codes){
		$savePath = C('REALPATH_SHOP').'/'.$shop_id.'/cate_brand';
		$data = F_CACHE($savePath);
		if(!$data){
			$sql = "select cate_code2 ,brand,count(*) as item_cnt from product_price pp inner join product_lib pl on pp.prod_id=pl.id where pp.is_show=1 and shop_id=$shop_id group by cate_code2,brand ";
			$mod = new Model();
			$dataDb = $mod->query($sql);
			$data = array();
			foreach($dataDb as $key=>$val){
				$data[$val['cate_code2']][]=array($val['brand'],$val['item_cnt']);
			}
			F_CACHE($savePath,$data);
			unset($dataDb);
		}
		$cateFilter = array();	//过滤相同的
		foreach ($cate_codes as $key=>$val){
			
			$cateBrands = $data[$val];
			if($cateBrands){
				foreach ($cateBrands as $subKey=>$subVal){
					if(!in_array($subVal[0], $cateFilter)){
						$cateFilter[]=$subVal[0];
						
					}
				}
			}
		}
	
		return $cateFilter;
	}
	
}

?>